//  this library.
import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter_hi_cache/flutter_hi_cache.dart';
import 'package:hktk_app/ienum/practice/ct_enum.dart';
import 'package:hktk_app/ienum/practice/del_collect_enum.dart';
import 'package:hktk_app/ienum/practice/one_do_source_enum.dart';
import 'package:hktk_app/model/practice/practice_model.dart';
import 'package:hktk_app/model/practice/practice_question_model.dart';
import 'package:hktk_app/model/practice/practice_section_model.dart';
import 'package:hktk_app/model/question/sz_info_model.dart';
import 'package:hktk_app/util/dao_util.dart';

/// 每日一练相关接口
class PracticeDao {
  /// 设置保存的key值
  static const String _szKey = "szKey";

  /// 今日每日一练
  static Future<PracticeModel> todayPractice({required int categoryId}) async {
    String url =
        "/exam/every-day-practice/today-practice?categoryId=$categoryId";
    // 发送请求
    var data = await httpGET(url: url, tips: "今日每日一练");
    return PracticeModel.fromJson(data);
  }

  /// 单道试题做题方法，
  /// id：试题id，
  /// oneDoSourceEnum：试题的来源，
  /// industryCode：行业id，
  /// examId：考试id，
  /// categoryId：科目id，
  /// ctEnum：未知，
  /// view：是否显示做题记录，
  /// cid：集合id，
  static Future<PracticeQuestionModel> clientOneDo({
    required String id,
    required OneDoSourceEnum oneDoSourceEnum,
    required String industryCode,
    required String examId,
    required int categoryId,
    required CtEnum ctEnum,
    required bool view,
    required String? cid,
  }) async {
    Map<String, dynamic> params = {
      "id": id,
      "oneDoSource": oneDoSourceEnum.name,
      "industryCode": industryCode,
      "examId": examId,
      "categoryId": categoryId,
      "ct": ctEnum.name,
      "view": view,
      "cid": cid,
    };
    // 请求路径
    String url = "/exam/question/client-one-do";
    // 发送请求
    var data = await httpPOST(
      url: url,
      params: params,
      tips: "单道试题做题",
      contentType: cTypeX,
    );
    return PracticeQuestionModel.fromJson(data);
  }

  /// 保存设置的相关信息
  static void saveSzInfo(SzInfoModel szInfoModel) {
    String encode = jsonEncode(szInfoModel);
    HiCache.getInstance().setString(_szKey, encode);
  }

  /// 获取保存的设置信息
  static SzInfoModel getSzInfo() {
    String? value = HiCache.getInstance().get(_szKey);
    if (value == null) {
      return SzInfoModel(zdtz: true, yjms: false, ztdx: 20.0);
    }

    Map<String, dynamic> decode = jsonDecode(value);

    return SzInfoModel.fromJson(decode);
  }

  // categoryId：科目id，
  // start：开始时间，
  // end：结束时间，
  /// 获取某个时间段的每日一练
  static Future<List<PracticeSectionModel>> sectionPractice({
    required int categoryId,
    String? start,
    String? end,
  }) async {
    String url =
        "/exam/every-day-practice/section-practice?categoryId=$categoryId";
    if (start != null) {
      url = "$url&start=$start";
    }
    if (end != null) {
      url = "$url&end=$end";
    }
    // 发送请求
    var data = await httpGET(url: url, tips: "获取某个时间段的每日一练");
    if (data is List<dynamic>) {
      return data.map((v) => PracticeSectionModel.fromJson(v)).toList();
    } else {
      return <PracticeSectionModel>[];
    }
  }

  /// 删除试题集合 收藏记录 或者错题记录
  static Future<bool> delChapterCollect({
    required String industryCode,
    required String examId,
    required int categoryId,
    required String collectId,
    required CtEnum recordsType,
    required DelCollectEnum type,
    required bool haveCollect,
  }) async {
    Map<String, dynamic> params = {
      "industryCode": industryCode,
      "examId": examId,
      "categoryId": categoryId,
      "collectId": collectId,
      "recordsType": recordsType.name,
      "type": type.name,
      "haveCollect": haveCollect,
    };
    String url = "/exam/records/del-chapter-collect";
    // 发送请求
    var data = await httpDelete(
      url: url,
      params: params,
      tips: "删除试题集合 收藏记录 或者错题记录",
    );
    return data == true;
  }

  /// 每日一练删除
  static Future<bool> everyDayDelete({required String id}) async {
    String url = "/exam/every-day-practice/delete/$id";
    // 发送请求
    var data = await httpGET(
      url: url,
      tips: "每日一练删除",
    );
    return data == true;
  }
}
